package cn.alm.allinoneboot.excel;

import cn.alm.allinoneboot.entity.Brand;
import cn.alm.allinoneboot.entity.Product;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Component
public class ExcelWriterHandler {

    @Autowired
    private HttpServletResponse response;

    public void writeBrand(List<Brand> list) throws IOException {
        // 设置response参数，可以下载文件
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("品牌信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), Brand.class).sheet("品牌信息").doWrite(list);
    }

    public void writeProduct(List<Product> list) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("产品信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Product.class).sheet("产品信息").doWrite(list);
    }


}
